﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace CP.Utility
{
    public static class Ascx_Reader
    {
        private static string GetActionName(string action)
        {
            string actionname = "";
            switch (action)
            {
                case "Query":
                    actionname = "查询";
                    break;
                case "AddNew":
                    actionname = "新增";
                    break;
                case "Edit":
                    actionname = "编辑";
                    break;
                case "Delete":
                    actionname = "删除";
                    break;
                case "Report":
                    actionname = "打印";
                    break;
                case "EditColumn":
                    actionname = "修改";
                    break;
                case "DeleteColumn":
                    actionname = "删除";
                    break;
                default:
                    actionname = "";
                    break;

            }
            return actionname;
        }
        private static string FindActionText(string zone, string Text, int serachIndex)
        {
            int endIndex = zone.IndexOf(">", serachIndex);
            int foundIndex = zone.IndexOf(Text, serachIndex, endIndex - serachIndex);
            if (foundIndex == -1)
                return "";

            int foundStart = zone.IndexOf("\"", foundIndex) + 1;
            int foundEnd = zone.IndexOf("\"", foundStart);
            string foundCommand = zone.Substring(foundStart, foundEnd - foundStart);
            return foundCommand;
        }
        private static NameValueCollection GetAction(NameValueCollection actions, string zone, string action, int start)
        {

            start = zone.IndexOf(":" + action, start);
            if (start != -1)
            {
                string founddatafield = FindActionText(zone, "DataField", start);
                string foundaction = FindActionText(zone, "Action", start);
                foundaction = string.IsNullOrEmpty(foundaction) ? action.Replace("Column", "") : foundaction;

                if (founddatafield != "") foundaction = founddatafield;

                string foundtext = FindActionText(zone, "Text", start);
                foundtext = string.IsNullOrEmpty(foundtext) ? GetActionName(action) : foundtext;
                actions[foundaction] = foundtext;

                GetAction(actions, zone, action, start + 2);
            }
            return actions;
        }

        public static void GetOperatorAction(NameValueCollection actions, string pageBuilder)
        {
            int startIndex = pageBuilder.IndexOf("<OperatorButtonsZone>");
            if (-1 == startIndex) return;
            int endIndex = pageBuilder.IndexOf("</OperatorButtonsZone>");
            if (-1 == endIndex) return;
            string zone = pageBuilder.Substring(startIndex, endIndex - startIndex + "</OperatorButtonsZone>".Length + 1);
            GetAction(actions, zone, "Query", 0);
            GetAction(actions, zone, "AddNew", 0);
            GetAction(actions, zone, "Delete", 0);
            GetAction(actions, zone, "Edit", 0);
            GetAction(actions, zone, "SubmitButton", 0);
            GetAction(actions, zone, "LinkButton", 0);
        }

        public static void GetGridZoneAction(NameValueCollection actions, string pageBuilder)
        {
            int startIndex = pageBuilder.IndexOf("<GridViewZone");
            if (-1 == startIndex) return;
            int endIndex = pageBuilder.IndexOf("</GridViewZone>");
            if (-1 == endIndex) return;
            string zone = pageBuilder.Substring(startIndex, endIndex - startIndex + "</GridViewZone>".Length + 1);
            GetAction(actions, zone, "EditColumn", 0);
            GetAction(actions, zone, "DeleteColumn", 0);
            GetAction(actions, zone, "CommandColumn", 0);
        }

        public static void GetGridViewZoneDetailsList(NameValueCollection actions, string pageBuilder)
        {
            int startIndex = pageBuilder.IndexOf("<GridViewZoneDetails>");
            if (startIndex > 0)
                actions["GridViewDetailsUpateable"] = "明细表操作";

        }

        public static bool GetLinkButtons(string pageBuilder)
        {
            int startIndex = pageBuilder.IndexOf(":LinkButton");
            if (startIndex > 0)
                return true;
            return false;

        }

        public static void GetGridZoneList(NameValueCollection actions, string pageBuilder)
        {
            int startIndex = pageBuilder.IndexOf("<GridViewZone");
            if (-1 == startIndex) return;
            int endIndex = pageBuilder.IndexOf("</GridViewZone>");
            if (-1 == endIndex) return;
            string zone = pageBuilder.Substring(startIndex, endIndex - startIndex + "</GridViewZone>".Length + 1);
            GetAction(actions, zone, "DataColumn", 0);
        }

        #region 临时使用
        private static IList<string> GetLinkUrl(IList<string> urls, string zone, string action, int start)
        {
            start = zone.IndexOf(":" + action, start);
            if (start != -1)
            {
                string foundtext = FindActionText(zone, "Url", start);
                if (!string.IsNullOrEmpty(foundtext))
                {
                    if (foundtext.IndexOf(".ascx") < 0)
                        foundtext = foundtext + ".ascx";
                    urls.Add(foundtext);
                }
                GetLinkUrl(urls, zone, action, start + 2);
            }
            return urls;
        }

        public static void GetLinkUrls(IList<string> urls, string pageBuilder)
        {
            int startIndex = pageBuilder.IndexOf("<OperatorButtonsZone>");
            if (-1 == startIndex) return;
            int endIndex = pageBuilder.IndexOf("</OperatorButtonsZone>");
            if (-1 == endIndex) return;
            string zone = pageBuilder.Substring(startIndex, endIndex - startIndex + "</OperatorButtonsZone>".Length + 1);
      
            GetLinkUrl(urls, zone, "LinkButton", 0);
        }
        #endregion
    }
}
